package org.apache.struts.pharmacy.pharmacy.action;

import java.util.Date;
import java.util.Map;

import org.apache.struts.pharmacy.pharmacy.model.HL7Message;
import org.apache.struts.pharmacy.pharmacy.model.RequestDrugs;

import com.earogya.communicate.Communicator;
import com.earogya.logic.pharmacy.request.CreateRDEO11Request;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/***
 * Class consists with the struts action to request drugs to the pharmacy.
 * @author klanerolle@gmail.com
 *
 */
public class RequestDrugsAction extends ActionSupport{
	private RequestDrugs requestDrugs;
	private HL7Message message;
	private Date date;

	public HL7Message getMessage() {
		return message;
	}

	/**
	 * Overrides the execute method is ActionSupport interface,When the action occurs this method is executed.
	 * returns SUCCESS if there are no errors.
	 */
	public String execute() throws Exception {
		Map<String, Object> session=ActionContext.getContext().getSession();
		CreateRDEO11Request createMessage=new CreateRDEO11Request(requestDrugs);
		String msg=createMessage.createMessage((String)session.get("facility"));
		message=new HL7Message();
		if(!msg.equals(null)){
			Communicator c=new Communicator();
			String reply=c.sendMessage(msg);
			message.setMessage(msg+"-------------"+reply);
		}
		else{
			message.setMessage("empty");
		}
		return SUCCESS;
	}
	
	public RequestDrugs getRequestDrugs() {
		return requestDrugs;
	}

	public void setRequestDrugs(RequestDrugs requestDrugs) {
		this.requestDrugs = requestDrugs;
	}

	/**
	 * Overrides the validate method in ActionSupport interface.
	 * Validates the input fields for correct input.
	 */
	public void validate(){
		if(requestDrugs.getRequestId().length()== 0){
			addFieldError( "requestDrugs.requestId", "Request ID required" );
		}else if(!this.isInteger(requestDrugs.getRequestId())){
			addFieldError( "requestDrugs.requestId", "Incorrect Request ID format." );
		}
		
		if(requestDrugs.getDrugId().length()== 0){
			addFieldError( "requestDrugs.drugId", "Drug ID required" );
		}

		if(requestDrugs.getMinAmount().length()== 0){
			addFieldError( "requestDrugs.minAmount", "Minimum Amount of Dosage required" );
		}else if(!this.isInteger(requestDrugs.getMinAmount())){
			addFieldError( "requestDrugs.minAmount", "Minimum Amount should be integer" );
		}
		
		if(requestDrugs.getMaxAmount().length()== 0){
			addFieldError( "requestDrugs.maxAmount", "Maximum Amount of Dosage required" );
		}else if(!this.isInteger(requestDrugs.getMaxAmount())){
			addFieldError( "requestDrugs.maxAmount", "Maximum Amount should be integer" );
		}else if(Integer.parseInt(requestDrugs.getMaxAmount())<Integer.parseInt(requestDrugs.getMinAmount())){
			addFieldError( "requestDrugs.maxAmount", "Maximum Amount should be higher than Min Amount" );
		}
		
		if(requestDrugs.getAmountUnits().length()== 0){
			addFieldError( "requestDrugs.amountUnits", "Amount/Units required" );
		}
		
		if(requestDrugs.getQuantity().length()== 0){
			addFieldError( "requestDrugs.quantity", "Quantity required" );
		}else if(!this.isInteger(requestDrugs.getQuantity())){
			addFieldError( "requestDrugs.quantity", "Incorrect Quantity format." );
		}
		
		if(requestDrugs.getUrgency().length()== 0){
			addFieldError( "requestDrugs.urgency", "Urgency is required" );
		}
		
		if(requestDrugs.getUnitOrWard().length()== 0){
			addFieldError( "requestDrugs.unitOrWard", "Unit Or Ward is required" );
		}
		
		if(requestDrugs.getEnteredByID().length()== 0){
			addFieldError( "requestDrugs.enteredByID", "Entered By Id required" );
		}else if(!this.isInteger(requestDrugs.getEnteredByID())){
			addFieldError( "requestDrugs.enteredByID", "Incorrect Entered By Id format." );
		}
		
		if(requestDrugs.getVerifiedByID().length()== 0){
			addFieldError( "requestDrugs.verifiedByID", "Verified By Id required" );
		}else if(!this.isInteger(requestDrugs.getVerifiedByID())){
			addFieldError( "requestDrugs.verifiedByID", "Incorrect Verified By Id format." );
		}
		
		if(requestDrugs.getRequestDate()== null){
			addFieldError( "requestDrugs.requestDate", "Request Date required" );
		}
	}
	
	/**
	 * Check if the given string is an integer.
	 * @param value
	 * @return true is the value is an integer
	 */
	private Boolean isInteger(String value){
		Boolean isInteger=false;
		try{
			int intValue=Integer.parseInt(value);
			isInteger=true;
		}catch(NumberFormatException e){
			isInteger=false;
		}
		return isInteger;
	}
}
